using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Universe.Farm.Builtin;
using Universe.Farm.Builtin.ComputerInfo;

namespace Universe.Farm.Tests
{
    class MyEnvironment
    {
        public static ConfigurationContainer CreateConfiguration(TimeSpan interval)
        {
            ConfigurationContainer config = new ConfigurationContainer();
            
            List<ServiceListenerConfiguration> listeners = new List<ServiceListenerConfiguration>();

            ServiceListenerConfiguration
                listener1 =
                    new ServiceListenerConfiguration(
                        "ComputerInfo",
                        interval,
                        new ComputerInfoHealth()),
                listener2 =
                    new ServiceListenerConfiguration(
                        "LocalSqlServer",
                            interval,
                        new MsSqlHealth());

            listeners.Add(listener1);
            listeners.Add(listener2);
            config.Register<IEnumerable<ServiceListenerConfiguration>>(delegate() { return listeners; });
            
            CommunicationParameters cp = CommunicationParameters.Default;
            config.Register<CommunicationParameters>(delegate { return cp; });

            return config;

        }

        public static bool Wait(IFarmService service, ServicesScope scope, TimeSpan timeOut)
        {
            DateTime start = DateTime.Now;
            while (true)
            {
                IList<HealthInfo> list = service.GetServices(scope);
                if (list.Count >= scope.Items.Count)
                    return true;

                if (DateTime.Now - start > timeOut)
                    return false;

                Thread.Sleep(100);
            }
        }

    }
}
